package com.sprucetec.live.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sprucetec.live.common.exceptioin.ParamInvalidException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DateUtil {

	public static final String DATE_TIME_SECOND_PATTERN = "yyyyMMddHHmmssSSS";
	public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN = "yyyy-MM-dd";
	public static final String DATE_MONTH_PATTERN = "yyyyMMddHHmmss";
	public static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00";
    public static final String DAY_END_STRING_HHMMSS = " 23:59:59";
    public static final String DATE_YEAR_M_PATTERN = "yyyy-M";
    public static final String DATE_YEAR_PATTERN = "yyyy";
    public static final String DATE_YEAR_MM_DD_PATTERN = "yyyy/MM/dd";

    public static String getDateTime(String datePattern, Date aDate) {
		if (aDate == null) {
			return null;
		}
		return (new SimpleDateFormat(datePattern)).format(aDate);
	}
    
    public static String convertDateToString(String datePattern, Date aDate) {
		return getDateTime(datePattern, aDate);
	}
    
    public static final Date convertStringToDate(String datePattern, String strDate) {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(datePattern);
        if (log.isDebugEnabled()) {
            log.debug("converting '" + strDate + "' to date with mask '" + datePattern + "'");
        }
        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            log.error("ParseException: " + pe);
        }
        return (date);
    }
    
    /**
	 * 获取指定日期（分钟）
	 * minutes 大于0表示推迟几分钟
	 * minutes 小于0表示提前几分钟
	 * @param date
	 * @param minutes
	 * @return
	 */
	public static Date addMinutes(Date date, int minutes) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minutes);
		return calendar.getTime();
	}
	
	/**
     * 获取指定日期（天）
     * day 大于0表示推后几天
     * day 小于0表示提前几天
     * @param date
     * @param day
     * @return
     */
    public static Date addDate(Date date,int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }
    
    /**
     * 获取指定日期（月）
     * month 大于0表示推后几个月
     * month 小于0表示提前几个月
     * @param date
     * @param day
     * @return
     */
    public static Date addMonth(Date date, int month) {
    	 Calendar calendar = Calendar.getInstance();
    	 calendar.setTime(date);
    	 calendar.add(Calendar.MONTH, month);
    	 return calendar.getTime();
    }
    
    /**
	 * 将long型的日期转化为Date型
	 * 
	 * @param time
	 * @return
	 */
	public static Date parseDate(long time) {
		Date rtnDate = new Date();
		rtnDate.setTime(time);
		return rtnDate;
	}
	
	/**
	 * 查询某年第一天
	 * @param year
	 * @return
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 某年最后一天
	 * @param year
	 * @return
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return currYearLast;
	}
	
	/**
	 * 某月第一天  
	 * @param month
	 * @return
	 */
	public static Date getMonthFirst(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		return calendar.getTime();
	}
	
	/**
	 * 某月最后一天
	 * @param month
	 * @return
	 */
	public static Date getMonthLast(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
	
	
	/**
	 * 某个季度第一天
	 * @param year	
	 * @param season
	 * @return
	 */
	public static Date getSeasonFirst(int year, int season){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		switch (season) {
		case 1:
			calendar.set(Calendar.MONTH, 0);
			break;
		case 2:
			calendar.set(Calendar.MONTH, 3);

			break;
		case 3:
			calendar.set(Calendar.MONTH, 6);

			break;
		case 4:
			calendar.set(Calendar.MONTH, 9);
			break;
		default	:
			throw new ParamInvalidException("season", "不存在的季度");
		}
		return calendar.getTime();
	}
	
	/**
	 * 某个季度最后一天
	 * @param year
	 * @param season
	 * @return
	 */
	public static Date getSeasonLast(int year, int season){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		switch (season) {
		case 1:
			calendar.set(Calendar.MONTH, 2);
			break;
		case 2:
			calendar.set(Calendar.MONTH, 5);

			break;
		case 3:
			calendar.set(Calendar.MONTH, 8);

			break;
		case 4:
			calendar.set(Calendar.MONTH, 11);
			break;
		default:
			throw new ParamInvalidException("season", "不存在的季度");
		}
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
		
	}
		
	/**
	 *  获取指定月份有多少天
	 * @param year	年份
	 * @param month	月份
	 * @return
	 */
	public static int getDaysByYearMonth(int year, int month) {  
        Calendar a = Calendar.getInstance();  
        a.set(Calendar.YEAR, year);  
        a.set(Calendar.MONTH, month - 1);  
        a.set(Calendar.DATE, 1);  
        a.roll(Calendar.DATE, -1);  
        int maxDate = a.get(Calendar.DATE);  
        return maxDate;  
    }

}
